// Copyright (C) 2021, Flickering Inc. All rights reserved.
// Author: haiquanchen (haiquanchen@flickering.ai)

const { ccclass, property } = cc._decorator;
@ccclass
export default class ECG extends cc.Component {
    @property({ type: Number, displayName: '宽度' })
    wide: number = 1;
    @property({ type: Number, displayName: '速度' })
    speed: number = 1;
    @property({ type: Number, displayName: '显示长度' })
    showlength: number = 1;

    @property({ type: cc.Color, displayName: '颜色' })
    color: cc.Color = cc.color(255, 255, 255);
    @property({ type: Number, displayName: '透明度' })
    opacity: number = 120;

    private _time: number = 0;
    private _material: cc.MaterialVariant;

    onLoad() {
        if (this.node.getComponent(cc.Label)) {
            this._material = this.node.getComponent(cc.Label).getMaterial(0);
        } else if (this.node.getComponent(cc.Sprite)) {
            this._material = this.node.getComponent(cc.Sprite).getMaterial(0);
        }
        this._material.setProperty('wide', this.wide);

        this._material.setProperty('showlength', this.showlength);
        this._material.setProperty('opacity', this.opacity / 255);
        this._material.setProperty('colorR', this.color.r / 255);
        this._material.setProperty('colorG', this.color.g / 255);
        this._material.setProperty('colorB', this.color.b / 255);
    }

    update(dt) {
        if (this._material) {
            if (this._time > 4) {
                this._time = 0;
            }

            this._material.setProperty('u_time', this._time);
            this._time += dt * this.speed;
        }
    }
}
